
var ModalEffects = (function() {

	function init() {

        // var overlay = document.querySelector( '.md-overlay' );

		[].slice.call( document.querySelectorAll( '.md-trigger' ) ).forEach( function( el, i ) {

			var modal = document.querySelector( '#' + el.getAttribute( 'data-modal' ) ),
				close = modal.querySelector( '.md-close' );
            var overlay = document.querySelector( '.md-overlay:not([data-modal])' );
			var currentOverlay = document.querySelector( '.md-overlay[data-modal=' + el.getAttribute( 'data-modal' )+']' );
			if(currentOverlay != undefined){
				// 找到了专属的overlay
				overlay = currentOverlay;
				// 看是否有z-index-plus
				var z_index_plus = overlay.getAttribute("z-index-plus");
				if(z_index_plus != undefined){
					// 如果有el和overlay的z-index都加上这个数
					var plus = parseInt(z_index_plus);
                    modal.style.zIndex = 9999999 + plus;
					overlay.style.zIndex = 9999995 + plus;
					// el.setAttribute("style","z-index:"+(9999999+ plus)+"px;");
				}
			}

			function removeModal( hasPerspective ) {
				classie.remove( modal, 'md-show' );
                overlay.style.visibility="hidden";

				if( hasPerspective ) {
					classie.remove( document.documentElement, 'md-perspective' );
				}
			}

			function removeModalHandler() {
				removeModal( classie.has( el, 'md-setperspective' ) );
			}

			el.addEventListener( 'click', function( ev ) {
				classie.add( modal, 'md-show' );
				overlay.style.visibility="visible";
				overlay.removeEventListener( 'click', removeModalHandler );
				overlay.addEventListener( 'click', removeModalHandler );

				if( classie.has( el, 'md-setperspective' ) ) {
					setTimeout( function() {
						classie.add( document.documentElement, 'md-perspective' );
					}, 25 );
				}
			});
			if(close != undefined) {
                close.addEventListener('click', function (ev) {
                    ev.stopPropagation();
                    removeModalHandler();
                });
            }

		} );

	}

	init();

})();